package ru.spb.osll.myfit.server.parser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.util.Vector;

import org.junit.Test;

import ru.spb.osll.myfit.common.Table;
import ru.spb.osll.myfit.common.TableCell;
import ru.spb.osll.myfit.common.TableCellResult;
import fit.Parse;

public class TableParserTest {
	private static final String IMPORTS_XML = "<table border=\"1\" cellspacing=\"0\">\n"
			+ "<tr><td>Import</td>\n"
			+ "</tr>\n"
			+ "<tr><td>ru.spb.osll.myfit.examples</td>\n"
			+ "</tr>\n"
			+ "</table>\n";

	// private static final String IMPORTS_TXT = "|Import|\n"
	// + "|ru.spb.osll.myfit.examples|\n";

	private static final String WITH_CUBETOOLS_XML = "<table border=\"1\" cellspacing=\"0\">\n"
			+ "<tr><td colspan=\"7\">with cubetools</td>\n"
			+ "</tr>\n"
			+ "<tr><td class=\"pass\">write</td>\n"
			+ "<td>1000000</td>\n"
			+ "<td class=\"pass\">files of size</td>\n"
			+ "<td>0</td>\n"
			+ "<td class=\"pass\">using</td>\n"
			+ "<td>80</td>\n"
			+ "<td class=\"pass\">threads</td>\n" + "</tr>\n" + "</table>\n";

	private static final String WITH_CUBETOOLS_TXT = "|with cubetools|\n"
			+ "|write|1000000|files of size|0|using|80|threads|\n";

	private static final String CLUSTER_LIST_XML = "<table border=\"1\" cellspacing=\"0\">\n"
			+ "<tr><td colspan=\"2\">cluster list</td>\n"
			+ "</tr>\n"
			+ "<tr><td class=\"error\">name<hr><pre><div class=\"fit_stacktrace\">Missing method: public ResultType nameAddress(Type1 name, Type2 address) { } in class ru.spb.osll.myfit.examples.ClusterListFixture</div></pre></hr></td>\n"
			+ "<td>address</td>\n"
			+ "</tr>\n"
			+ "<tr><td class=\"ignore\">my cluster</td>\n"
			+ "<td class=\"ignore\">127.0.0.1</td>\n"
			+ "</tr>\n"
			+ "</table>\n";

	private static final String CLUSTER_LIST_TXT = "|cluster list|\n"
			+ "|name|address|\n" + "|my cluster|127.0.0.1|\n";

	private TableParser tableParser = new TableParser();

	@Test
	public void testParseImport() throws Exception {
		assertNull(tableParser.parse(new Parse(IMPORTS_XML)));
	}

	@Test
	public void testParseDoFixture() throws Exception {
		Table table = tableParser.parse(new Parse(WITH_CUBETOOLS_XML));
		String str = PageParserUtils.tableToString(table).replaceAll("\r", "");
		assertEquals(WITH_CUBETOOLS_TXT, str);
		assertStats(table, 0, 0, 4);
	}

	@Test
	public void testParseClusterList() throws Exception {
		Table table = tableParser.parse(new Parse(CLUSTER_LIST_XML));
		String str = PageParserUtils.tableToString(table).replaceAll("\r", "");
		assertEquals(CLUSTER_LIST_TXT, str);
		assertStats(table, 1, 0, 0);
		assertException(
				table,
				"Missing method: public ResultType nameAddress(Type1 name, Type2 address) { } in class ru.spb.osll.myfit.examples.ClusterListFixture");
	}

	@Test
	public void testParseTwoTables() throws Exception {
		Table table = tableParser.parse(new Parse(CLUSTER_LIST_XML
				+ WITH_CUBETOOLS_XML));
		String str = PageParserUtils.tableToString(table).replaceAll("\r", "");
		assertEquals(CLUSTER_LIST_TXT, str);
		assertStats(table, 1, 0, 0);
		assertException(
				table,
				"Missing method: public ResultType nameAddress(Type1 name, Type2 address) { } in class ru.spb.osll.myfit.examples.ClusterListFixture");
	}

	private void assertStats(Table table, int exception, int fail, int pass) {
		int actualException = 0;
		int actualFail = 0;
		int actualPass = 0;

		for (Vector<TableCell> row : table.getData()) {
			for (TableCell cell : row) {
				if (cell.getTestResult() == TableCellResult.EXCEPTION) {
					actualException = actualException + 1;
				} else if (cell.getTestResult() == TableCellResult.FAIL) {
					actualFail = actualFail + 1;
				} else if (cell.getTestResult() == TableCellResult.PASS) {
					actualPass = actualPass + 1;
				}
			}
		}
		assertEquals(exception, actualException);
		assertEquals(fail, actualFail);
		assertEquals(pass, actualPass);
	}

	private void assertException(Table table, String exception) {
		for (Vector<TableCell> row : table.getData()) {
			for (TableCell cell : row) {
				if (cell.getTestResult() == TableCellResult.EXCEPTION) {
					assertEquals(exception, cell.getTooltip());
				}
			}
		}
	}

}
